﻿@model NCLCustomerManagement.UI.Models.SaleModel

@{
    ViewBag.Title = "Thêm bán hàng";
    var currentUser = Session[NCLCustomerManagement.Common.Constant.Constant.CURRENT_USER]
                       as NCLCustomerManagement.UI.Models.User.AuthenticatedUser;
    var username = currentUser.Username;
    var isAdmin = currentUser.IsAdmin;                       
}



<script>

    function normalizeNumber(num) {
        var a = num.toString();
        a = a.replace(",", "");
        return a;
    }
    function selectCustomer() {
        GetCurrentLoginUser(function (data) {
            if (!data || !data.Username || data.Username === '')
                window.location.href = '/User/Login';
            //if (!data.IsAdmin) {
            //    window.location.href = '/User/Login';
            //}
            $.blockUI();
            $.ajax({
                url: "/Customer/SelectCustomer",
                data: {},
                contentType: 'text/html',
                dataType: "html",
                type: "get",
                success: function (data) {
                    $("#editWindow .modal-body").html(data);
                    $('#editWindow .modal-title').html("Chọn khách hàng");
                    $('#editWindow').modal({ backdrop: false });

                    $("#editWindow .modal-dialog").css({ width: 1000, height: 500 });

                    $.unblockUI();
                },
                error: function (data) {
                    alert("Yêu cầu đăng nhập lại");
                    $.unblockUI();
                }
            });

        });
    }
    function selectProduct() {
        //GetCurrentLoginUser(function (data) {
        //    if (!data || !data.Username || data.Username === '')
        //        window.location.href = '/User/Login';
        //    //if (!data.IsAdmin) {
        //    //    window.location.href = '/User/Login';
        //    //}
        //    $.blockUI();
        //    $.ajax({
        //        url: "/Product/RenderSelectProductView",
        //        data: {},
        //        contentType: 'text/html',
        //        dataType: "html",
        //        type: "get",
        //        success: function (data) {
        //            $("#editWindow .modal-body").html(data);
        //            $('#editWindow .modal-title').html("Chọn sản phẩm");
        //            $('#editWindow').modal({ backdrop: false });

        //            $("#editWindow .modal-dialog").css({ width: 1000, height: 500 });

        //            $.unblockUI();
        //        },
        //        error: function (data) {
        //            alert("Yêu cầu đăng nhập lại");
        //            $.unblockUI();
        //        }
        //    });

        //});

        var data = '<iframe id="productFrame" src="/Product/" style="width:100%; height:500px;"></iframe>';
        data += '<div style="text-align: right;">' +
                '<button type="button" class="btn btn-default" onclick="onProductSelected();">Chọn</button>' +
                '</div>';
        $("#editWindow .modal-body").html(data);
        $('#editWindow .modal-title').html("Chọn sản phẩm");
        $('#editWindow').modal({ backdrop: false });

        $("#editWindow .modal-dialog").css({ width: 1100 });
    }

    function onProductSelected() {
        var obj = $("#productFrame").contents();
        var selectedObj = obj.find("input[type='radio'].product-select:checked");

        if (isNaN(selectedObj.val())) {
            alert("Bạn hãy chọn sản phẩm");
            return;
        }
        var id = $(selectedObj).val();
        var nameObj = $(selectedObj).next();
        var unitObj = nameObj.next();
        var unitPriceObj = unitObj.next();

        $("#ProductId").val(id);
        $("#ProductName").val(nameObj.val());
        $("#Unit").val(unitObj.val());
        $("#UnitPrice").val(unitPriceObj.val());

        calcTotalValue();
        $('#editWindow').modal('hide');
    }
</script>

@section title{
Bán hàng
}


@using (Html.BeginForm("New", "Sale", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()

    <div class="form-group">
        <div class="row">
            @Html.LabelFor(model => model.CustomerName, new { @class = "col-xs-2 control-label" })
            <div class="col-xs-8">

                @Html.TextBoxFor(model => model.CustomerName, new { @class = "form-control input-sm", placeholder = "Chọn khách hàng", disabled = "disabled" })
                @Html.HiddenFor(model => model.CustomerId)
            </div>
            <div class="col-xs-2">
                <button type="button" class="btn btn-default" onclick="selectCustomer();">...</button>
            </div>
        </div>
        <div class="row">
            @Html.LabelFor(model => model.ProductName, new { @class = "col-xs-2 control-label" })
            <div class="col-xs-8">

                @Html.TextBoxFor(model => model.ProductName, new { @class = "form-control input-sm", placeholder = "Chọn sản phẩm", disabled = "disabled" })
                @Html.HiddenFor(model => model.ProductId)
            </div>
            <div class="col-xs-2">
                <button type="button" class="btn btn-default" onclick="selectProduct();">...</button>
            </div>
        </div>
        <div class="row">
            @Html.LabelFor(model => model.Unit, new { @class = "col-xs-2 control-label" })
            <div class="col-xs-8">

                @Html.TextBoxFor(model => model.Unit, new { @class = "form-control input-sm", placeholder = "Đơn vị", disabled = "disabled" })
            </div>
        </div>
        <div class="row" style="margin-top: 5px;">
            @Html.LabelFor(model => model.UnitPrice, new { @class = "col-xs-2 control-label" })
            <div class="col-xs-8">

                @Html.TextBoxFor(model => model.UnitPrice, new { @class = "form-control input-sm computeTotalValue", placeholder = "Đơn giá" })
            </div>
        </div>
        <div class="row" style="margin-top: 5px;">
            @Html.LabelFor(model => model.Quantity, new { @class = "col-xs-2 control-label" })
            <div class="col-xs-8">

                @Html.TextBoxFor(model => model.Quantity, new { @class = "form-control input-sm computeTotalValue", placeholder = "Số lượng" })
            </div>
        </div>
        <div class="row" style="margin-top: 5px;">
            <label for="Tax" class="col-xs-2 control-label">Thuế (%)</label>
            <div class="col-xs-8">

                @Html.TextBoxFor(model => model.Tax, new { @class = "form-control input-sm computeTotalValue", placeholder = "Thuế" })
            </div>
        </div>
        <div class="row" style="margin-top: 5px;">
            <label for="Tax" class="col-xs-2 control-label">Chiết khấu (VND)</label>
            <div class="col-xs-8">

                @Html.TextBoxFor(model => model.Discount, new { @class = "form-control input-sm computeTotalValue", placeholder = "Chiết khấu" })
            </div>
        </div>
        <div class="row" style="margin-top: 5px;">
            <label for="Tax" class="col-xs-2 control-label">Thành tiền (VND)</label>
            <div class="col-xs-8">

                <span class="form-control" id="TotalValue">@(Model != null ? @Model.TotalValue : 0)</span>

            </div>
        </div>
    </div>
        
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-8" style="padding-left: 5px;">
            @Html.ValidationSummary()
            <button type="submit" class="btn btn-default">Lưu</button>
        </div>
    </div>
    
}


<div>
    @Html.ActionLink("Trở lại", "Index")
</div>

<div id="editWindow" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Modal title</h4>
            </div>
            <div class="modal-body">
                <p>One fine body&hellip;</p>
            </div>

        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->


@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

}
<script>
    $(document).ready(function () {

        $(".computeTotalValue").change(function () {

            calcTotalValue();

        });

    });

    function calcTotalValue() {


        var qty = $("#Quantity").val();
        var unitPrice = $("#UnitPrice").val();
        var taxP = $("#Tax").val();
        var taxValue = taxP / 100 * (qty * unitPrice);
        var discount = $("#Discount").val();

        var totalVal = (qty * unitPrice) + taxValue - discount;
        $("#TotalValue").text(totalVal);

    }
</script>
